package com.hdyg.sixg.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import com.hdyg.sixg.BuildConfig;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.AppManager;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.MainMenuAdapter;
import com.hdyg.sixg.adapter.base.MyFragmentAdapter;
import com.hdyg.sixg.entry.NativeGuideBean;
import com.hdyg.sixg.entry.VersionBean;
import com.hdyg.sixg.fragment.ApplicationFragment;
import com.hdyg.sixg.fragment.BaseFragment;
import com.hdyg.sixg.fragment.BiBiFragment;
import com.hdyg.sixg.fragment.MainFragment;
import com.hdyg.sixg.fragment.MineFragment;
import com.hdyg.sixg.fragment.MiniMachineFragment;
import com.hdyg.sixg.mvp.Contrant.CMain;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PMain;
import com.hdyg.sixg.ui.NoPreloadViewPager;
import com.hdyg.sixg.util.DataCenter;
import com.hdyg.sixg.util.StringUtil;
import com.hdyg.sixg.util.VersionUtil.AppDownloadManager;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author CZB
 * @describe 首页
 * @time 2019/4/3 11:30
 */
public class MainActivity extends BaseActivity implements CMain.IVMain {

    @BindView(R.id.view_page)
    NoPreloadViewPager viewPage;
    @BindView(R.id.rv_bottom)
    RecyclerView rvBottom;
    @BindView(R.id.iv_center)
    ImageView ivCenter;

    private List<NativeGuideBean> bottomDatas;
    private MainMenuAdapter bottomAdapter;

    private List<BaseFragment> pageDatas;
    private MyFragmentAdapter pageAdapter;
    private long mExitTime;
    private CMain.IPMain mPresenter;
    private String versionName;
    private AppDownloadManager mDownLoadManage;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mDownLoadManage = new AppDownloadManager(this);
        versionName = BuildConfig.VERSION_NAME;
        rvBottom.setLayoutManager(new GridLayoutManager(mContext,5));
        bottomDatas = DataCenter.getBottomData();
        initTabAdapter();
        initPage();
    }

    @Override
    protected void initData() {
        mPresenter.pGetVersion(BaseUrl.VERSION_URL,GetParamUtil.getVersionParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PMain(this);
    }

    //初始化底部
    private void initTabAdapter() {
        bottomAdapter = new MainMenuAdapter(R.layout.item_main_menu,bottomDatas);
        rvBottom.setAdapter(bottomAdapter);
        bottomAdapter.setOnItemClickListener((adapter, view, position) -> {
//            if (position == 1){
//                toastNotifyShort(R.string.toast_development);
//                return;
//            }
            if (position != 2){
                changeBottomUI(position);
            }
        });
    }

    private void changeBottomUI(int position){
        for (int i=0;i<bottomDatas.size();i++){
            if (i == position){
                bottomDatas.get(i).setSelect(true);
            }else {
                bottomDatas.get(i).setSelect(false);
            }
        }
        bottomAdapter.replaceData(bottomDatas);
        bottomAdapter.notifyDataSetChanged();
        viewPage.setCurrentItem(position);
    }
    //初始化首页fragment
    private void initPage(){
        if (pageDatas == null){
            pageDatas = new ArrayList<>();
        }else {
            pageDatas.clear();
        }
        pageDatas.add(new MainFragment());
//        pageDatas.add(new TransFragment());
        pageDatas.add(new MiniMachineFragment());
        pageDatas.add(new BiBiFragment());
        pageDatas.add(new ApplicationFragment());
        pageDatas.add(new MineFragment());
        pageAdapter = new MyFragmentAdapter(getSupportFragmentManager(), pageDatas);
        viewPage.setAdapter(pageAdapter);
    }

    @OnClick({R.id.iv_center})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_center:
                changeBottomUI(2);
                viewPage.setCurrentItem(2);
                break;
        }
    }

    // 双击退出app
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                toastNotifyShort(R.string.sys_out_login);
                mExitTime = System.currentTimeMillis();
            } else {
                StringUtil.clearAllCache(mContext);
                AppManager.getAppManager().AppExit(mContext);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void vGetVersionSuccess(VersionBean dataBean) {
        if (!versionName.equals(dataBean.getVersion())) {
            mDownLoadManage.showNoticeDialog(dataBean.getUrl(), getResources().getString(R.string.sys_version_title2), dataBean.getNote());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mDownLoadManage != null) {
            mDownLoadManage.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mDownLoadManage != null) {
            mDownLoadManage.onPause();
        }
    }
}
